<template>
	<view class="container" :style="'min-height:' +pageHei+';'">
		<!-- #ifdef H5 -->
		<u-sticky :customNavHeight="0">
			<view class="" style="background-color:#F7F8FC;padding: 10rpx;">
				<image class="u-m-x-20" v-if="windowUrl" @click="$store.commit('toWindowUrl')"
					src="https://job.ydylmold.cn/image/home-fill.png" style="width: 50rpx;" mode="widthFix">
				</image>
			</view>
		</u-sticky>
		<!-- #endif -->
		<!-- 轮播图 -->
		<view style="height: 344rpx;" class="u-m-20" v-if="list3 && list3.length">
			<u-swiper height="172" @click="clickSwi" :list="list3" interval="6000" duration="2000" previousMargin="20"
				nextMargin="20" circular :autoplay="true" radius="5"></u-swiper>
		</view>
		<!-- offsetTop="0"  :customNavHeight="windowUrl?98:58"-->
		<!-- #ifdef H5 -->
		<u-sticky :customNavHeight="30">
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<u-sticky :customNavHeight="0">
				<!-- #endif -->
				<!-- 搜索框 -->
				<view style="width: 100%;background-color:#F7F8FC;" class="u-p-y-6 ">
					<view @click="clickSearch" class=" searchBox u-m-t-40 u-m-b-20 u-flex u-row-between"
						style="background-color: #1DA9FF;">
						<view class="b-ll-44 u-font-24 u-weight-500 u-flex u-m-l-4 u-flex-1"
							style="height: 74rpx;background-color: #fff;border-radius: 44rpx 0 0 44rpx;">
							<text style="font-size: 28rpx;color: #a8a8a8;margin-left: 20rpx;">输入昵称, 企业, 行业等</text>
						</view>
						<view class="searchtext">
							<u-icon name="search" color="#ffffff" size="26"></u-icon>
						</view>
					</view>
				</view>
				<view class="select_down u-flex" style=" background-color: #F7F8FC; font-size: 30rpx;color: #666;">
					<view class="down" style="flex: 1;">
						<uni-data-picker seltype="hy" @changeCurrData="changeCurrData" :currData="currData"
							:showAll="true" placeholder="请选择行业" popup-title="选择行业" @change="handleChangeHy">
							<view class="menu_type">
								<view class="title">
									<text class="label">{{showHyText?showHyText:'行业'}}</text>
									<u-icon name="arrow-down">
									</u-icon>
								</view>
							</view>
						</uni-data-picker>
						<uni-data-picker seltype="city" @changeCityData="changeCityData" :showAll="true"
							:currDataCity="currDataCity" placeholder="请选择地区" popup-title="选择地区"
							@change="handleChangeAddress">
							<view class="menu_type">
								<view class="title">
									<text class="label">{{showAddr?showAddr:'地区'}}</text>
									<u-icon name="arrow-down">
									</u-icon>
								</view>
							</view>
						</uni-data-picker>
					</view>
				</view>
			</u-sticky>
			<view class="box-list" style="padding: 0 20rpx;margin-top: 10rpx;">
				<my-card @clickCard="toUrl" :isApply="item.isApply" @rightCilck="cardRightCilck" leftText="极速联系"
					:rightText="rightText" acWidth="345rpx" v-for="(item,index) in acList" :key="index" :acItem="item"
					:acIndex="index"></my-card>
			</view>
			<!-- <my-empty tipText="数据为空" marginTop="0rpx" image="http://cdn.uviewui.com/uview/empty/data.png"
				v-if="isEmpty">
			</my-empty>
			<view v-if="(isEmpty || status == 'nomore') && !assoId"
				style="color: #666666;text-align: center;margin-top: 40rpx;"> —— 为您推荐 ——</view> -->
			<view class="box-list" v-if="!assoId" style="padding: 20rpx 20rpx;margin-top: 10rpx;">
				<my-card @clickCard="toUrl" :isApply="item.isApply" @rightCilck="cardRightCilck" leftText="极速联系"
					:rightText="rightText" acWidth="345rpx" v-for="(item,index) in recommData" :key="index" :acItem="item"
					:acIndex="index"></my-card>
			</view>
			<u-loadmore :status="statusRec" v-if="showStatus"></u-loadmore>
			<u-loadmore :status="status" v-else></u-loadmore>
			<my-auth-modal></my-auth-modal>
			<view class="top-box" @click="scrollTop = 0">
				<u-back-top :scroll-top="scrollTop" top="600"></u-back-top>
			</view>
	</view>
</template>
<script>
	import {
		duplicateRemoval,
		addFriend
	} from '@/utils/common.js';
	import {
		querySysContacts,
		queryMsgForListNew,
		querySearchList,
		queryRandomFriend,
		queryRandomFriendComp,
	} from '@/common/api/msg.js';
	import {
		userVipSearch
	} from '@/common/api/pay.js';
	import {
		getToken
	} from '@/utils/token.js';
	import {
		queryEnterpriseContactComp,
		queryEnterpriseContact,
		queryPicListPublic
	} from '@/common/api/auth.js';
	import store from '@/store/index.js';
	export default {
		data() {
			return {
				list3: [],
				//查询条件
				form: {
					address: '',
					jobText: '',
					hyText: ''
				},
				pageHei: '100vh',
				jobList: [],
				currPage: 1,
				currPageRam: 1,
				acList: [],
				isEmpty: false,
				status: 'loading',
				statusRec: 'loading',
				recommData: [],
				btnText: '',
				showStatus: false,
				showHyText: '',
				acHyText: '',
				showAddr: '',
				swiperList: [],
				assoId: getToken('assoId'),
				version: 1,
				rightText: '+好友',
				scrollTop: 0,
				windowUrl: getToken('windowUrl'),
				//选择框
				currData: {},
				currDataCity: {}
			}
		},
		// 触发下拉刷新
		onPullDownRefresh() {
			this.init();
			// 延迟关闭刷新动画
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onShow: function() {
			// if(getToken('AUTH_TYPE'))
			// 	store.state.user.authType = getToken('AUTH_TYPE');
			uni.showTabBar();
			if (this.version != getToken('version')) {
				this.init();
				this.version = getToken('version');
			}
			if (getToken('version') == 1) {
				uni.setNavigationBarTitle({
					title: '人脉'
				})
				uni.setTabBarItem({
					index: 1,
					text: '人脉'
				})
				this.rightText = '+好友';
			} else {
				uni.setNavigationBarTitle({
					title: '企脉'
				})
				uni.setTabBarItem({
					index: 1,
					text: '企脉'
				})
				this.rightText = '+企友';
			}
			this.$store.dispatch('getUnReadListByEntity');
		},
		onLoad: function() {
			uni.getSystemInfo({
				success: (res) => {
					this.pageHei = res.windowHeight * 2 + 'rpx';
				}
			})
			this.init();
		},
		onReachBottom: function() {
			if (getToken('assoId')) {
				if (this.status == 'loadmore' && !this.getRam) {
					this.currPage++;
					this.queryMsgForListNew();
				}
			} else {
				if (this.isEmpty || this.status == 'nomore') {
					this.statusRec = 'loading';
					this.queryRandomFriend();
				} else {
					if (this.status == 'loadmore' && !this.getRam) {
						this.currPage++;
						this.queryMsgForListNew();
					}
				}
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		methods: {
			clickSwi(e) {
				//console.log("e", this.swiperList[e].picUrl)
				if (this.swiperList[e].picUrl.includes('https://job.ydylmold.cn/index.html#/pages/index/index')) {
					//#ifdef MP-WEIXIN
					uni.navigateTo({
						url: `/pages/comment/loginWeb?title=轮播图&url=${ this.swiperList[e].picUrl+"?isweixin=1"}`
					})
					//#endif
					// #ifdef H5
					uni.navigateTo({
						url: `/pages/comment/loginWeb?title=轮播图&url=${ this.swiperList[e].picUrl+"?isweixin=null"}`
					})
					// #endif
				} else {
					uni.navigateTo({
						url: `/pages/comment/loginWeb?title=轮播图&url=${this.swiperList[e].picUrl}`
					})
				}
			},
			clickSearch() {
				uni.$u.route({
					url: 'pages/comment/searchPage/search'
				})
			},
			toUrl(data) {
				this.$store.commit('toUserIndex', data.item);
			},
			//添加好友
			cardRightCilck(e) {
				let useId = getToken('version') == 1 ? (e.item && e.item.craftsmanId) : (e.item && e.item.companyId);
				let idx = e.index;
				this.$store.dispatch('toAddPeople', {
					useNum: useId,
					setType: 3,
					isApply: e.item.isApply
				}).then(res => {
					if (res) { //有次数，添加好友
						addFriend(useId).then(isAdd => {
							if (isAdd) {
								let itemAc = this.acList[idx];
								let itemRe = this.recommData[idx];
								if (getToken('version') == 1) {
									if (useId == itemAc.craftsmanId) {
										itemAc.isApply = true;
										this.$set(this.acList, idx, itemAc);
									} else {
										itemRe.isApply = true;
										this.$set(this.acList, idx, itemRe);
									}
								} else {
									if (useId == itemAc.companyId) {
										itemAc.isApply = true;
										this.$set(this.acList, idx, itemAc);
									} else {
										itemRe.isApply = true;
										this.$set(this.acList, idx, itemRe);
									}
								}
							}
						})
					}
				})
			},
			getNewList() {
				this.currPage = 1;
				this.acList = [];
				this.queryMsgForListNew();
			},
			// 查找推荐好友
			queryRandomFriend() {
				this.showStatus = true;
				if (getToken('version') == 1) {
					queryRandomFriend({
						craftsmanId: getToken('useNum'),
						mainBusiness: this.form.hyText,
						state: 1
					}).then(res => {
						if (res.data && res.data.code == 1) {
							this.recommData = this.recommData.concat(res.data.listData);
							this.statusRec = 'loadmore';
						}
					})
				} else { //企业推荐好友
					queryRandomFriendComp({
						companyId: getToken('useNum'),
						industryTypeName: this.form.hyText,
						state: 1
					}).then(res => {
						if (res.data && res.data.code == 1) {
							this.recommData = this.recommData.concat(res.data.listData);
							this.statusRec = 'loadmore';
						}
					})
				}
			},
			// 查找行业好友
			queryMsgForListNew() {
				this.status = 'loading';
				if (getToken('version') == 1) {
					let params = {
						craftsmanId: getToken('useNum'),
						mainBusiness: this.form.hyText,
						searchText: this.form.jobText,
						address: this.form.address,
						'page.current': this.currPage,
						state: 1
					}
					if (getToken('assoId')) {
						params['assoId'] = getToken('assoId');
					}
					queryMsgForListNew(params).then(res => {
						if (res.data) {
							let currList = res.data && res.data.rows;
							currList && currList.forEach(m => {
								if (m.mainBusiness) {
									let inList = m.mainBusiness.split(' ');
									let inStr = '';
									if (inList.length >= 4) {
										inStr = inList[2] + ' ' + inList[3];
									} else if (inList.length > 2 && inList.length < 4) {
										inStr = inList[1] + ' ' + inList[2];
									} else {
										inStr = inList[0] + ' ' + inList[1];
									}
									if (inStr) {
										m.mainBusiness = inStr;
									}
								}
							})
							this.acList = duplicateRemoval(this.acList, currList || [], 'craftsmanId');
							this.allpage = res.data && res.data.total;
							this.currPage < this.allpage ? this.status = 'loadmore' : this.status = 'nomore';
							this.isEmpty = this.acList.length == 0 ? true : false;
							if (this.isEmpty || status == 'nomore') {
								this.queryRandomFriend();
							}
						}
					})
				} else {
					let params = {
						companyNo: getToken('useNum'),
						industryTypeName: this.form.hyText,
						searchText: this.form.jobText,
						address: this.form.address,
						'page.current': this.currPage,
						state: 1
					}
					if (getToken('assoId')) {
						params['assoId'] = getToken('assoId');
					}
					querySearchList(params).then(res => {
						if (res.data) {
							let currList = res.data && res.data.rows;
							currList && currList.forEach(m => {
								if (m.industryTypeName) {
									let inList = m.industryTypeName.split(' ');
									let inStr = '';
									if (inList.length >= 4) {
										inStr = inList[2] + ' ' + inList[3];
									} else if (inList.length > 2 && inList.length < 4) {
										inStr = inList[1] + ' ' + inList[2];
									} else {
										inStr = inList[0] + ' ' + inList[1];
									}
									if (inStr) {
										m.industryTypeName = inStr;
									}
								}
							})
							this.acList = duplicateRemoval(this.acList, currList || [], 'companyId');
							this.allpage = res.data && res.data.total;
							this.currPage < this.allpage ? this.status = 'loadmore' : this.status = 'nomore';
							this.isEmpty = this.acList.length == 0 ? true : false;
							if (this.isEmpty || status == 'nomore') {
								this.queryRandomFriend();
							}
						}
					})
				}
			},
			//改变行业选择 数据
			changeCurrData(e) {
				//console.log('行业', e)
				this.currData = e
			},
			// 改变城市选择数据
			changeCityData(e) {
				this.currDataCity = e
			},
			//查找行业类别
			querySysContacts(params) {
				querySysContacts(params).then(res => {
					if (res.data && res.data.code == 1) {
						let jobArr = res.data.listData
						let arr = []
						jobArr.forEach((m1, idx1) => {
							arr.push({
								text: m1.keyword,
								children: [],
								value: m1.id
							})
							if (m1.sysContactsList && m1.sysContactsList.length) {
								m1.sysContactsList.forEach((m2, idx2) => {
									arr[idx1].children.push({
										text: m2.keyword,
										children: [],
										value: m2.id
									})
								})
							}
						})
						this.jobList = arr
					}
				})
			},
			// 初始化数据
			async init() {
				this.currPage = 1
				this.currPageRam = 1
				this.acList = []
				this.recommData = []
				this.jobList = []
				//获取企业轮播图、轮播图数量、企业认证状态、企业更多资料
				let result = {}
				let acTextHy = ''
				if (getToken('version') == 1) {
					result = await this.queryEnterpriseContact();
					if (result.data && result.data.mainBusiness) {
						acTextHy = result.data.mainBusiness;
					}
				} else {
					result = await this.queryEnterpriseContactComp();
					if (result.data && result.data.industryTypeName) {
						acTextHy = result.data.industryTypeName;
					}
				}
				let arr = acTextHy.split(' ').slice(0, 2)
				// this.form.hyText = arr.join(' ')
				this.acHyText = arr.join(' ')
				let params = {}
				if (getToken('version') == 1) {
					params = {
						belongTo: 1,
						industryName: arr.join(' ')
					}
					this.querySysContacts(params)
				} else {
					params = {
						belongTo: 2,
						industryName: arr.join(' ')
					}
					this.querySysContacts(params);
				}
				this.queryMsgForListNew()
				//获取轮播图
				this.queryPicListPublic()
			},
			queryPicListPublic() {
				queryPicListPublic({
					moduleNo: 'renmai',
					state: 1,
					owner: 'ydyl'
				}).then(res => {
					if (res.data && res.data.length) {
						this.swiperList = res.data
						//console.log("轮播图", res.data)
						this.list3 = res.data.map(v => {
							return 'https://www.ydylmold.cn/images/ydylapp/usrMainPic/usrCraftsmanImage/' +
								v.picPath
						})
					}
				})
			},
			//公司信息
			queryEnterpriseContact() {
				return queryEnterpriseContact({
					userId: getToken('useNum'),
					craftsmanId: getToken('useNum')
				})
			},
			queryEnterpriseContactComp() {
				return queryEnterpriseContactComp({
					companyNo: getToken('useNum')
				})
			},
			//地址选择回调处理
			handleChangeAddress(data) {
				let arr = data.detail.value.map(s => s.text);
				this.showAddr = arr && arr.length && arr[arr.length - 1] || '';
				this.form.address = arr.join(' ');
				this.getNewList();
			},
			handleChangeHy(data) {
				//console.log('行业data', data)
				let arr = data.detail.value.map(s => s.text); //循环获取每一级的text
				this.form.hyText = arr.join(' ');
				this.showHyText = arr && arr.length && arr[arr.length - 1] || ''; //获取最后一级text
				this.getNewList(); //筛选
			}
		}
	}
</script>
<style lang="scss">
	.container {
		// padding-top: 30rpx;
		background-color: #F7F8FC;
	}

	.searchBox {
		margin: 0 30rpx;
		height: 82rpx;
		border-radius: 44rpx;
		align-items: center;

		// padding: 0 12rpx;
		.searchtext {
			margin: 0 26rpx;
			font-size: 48rpx;
			font-weight: 400;
			color: rgba(255, 255, 255, 1);

			.stext {
				text-shadow: 0rpx 6rpx 10rpx 0rpx rgba(0, 0, 0, 0.25);
			}
		}
	}

	.box-list {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: space-between;
	}

	.down {
		height: 100%;
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		flex-wrap: nowrap;

		.menu_type {
			height: 80rpx;

			.title {
				height: 100%;
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				color: #666;
			}
		}
	}
</style>